package com.base.imgs.glide.transformation;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Shader;
import android.support.annotation.NonNull;

import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.TransformationUtils;

import jp.wasabeef.glide.transformations.BitmapTransformation;

public class ArcReflectedTransformation extends BitmapTransformation {

    private int quadHeight;
    private final Paint mPaint;
    private final Path mPath;

    public ArcReflectedTransformation() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPath = new Path();
    }

    public ArcReflectedTransformation(int quadHeight) {
        this();
        this.quadHeight = quadHeight;
    }

    @Override
    public String key() {
        return "ArcReflectedTransformation";
    }

    @Override
    protected Bitmap transform(@NonNull Context context, @NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth, int outHeight) {

        Bitmap bitmap = TransformationUtils.centerCrop(pool, toTransform, outWidth, outHeight);
        bitmap.setHasAlpha(true);

        int width = bitmap.getWidth();
        int height = bitmap.getHeight();

        Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

        mPath.moveTo(0, 0);
        mPath.addRect(0, 0, width, height - quadHeight, Path.Direction.CW);


        mPath.moveTo(0, height - quadHeight);
        mPath.quadTo(width / 2, height, width, height - quadHeight);

        Canvas canvas = new Canvas(result);

        Shader shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP,
                Shader.TileMode.CLAMP);
        mPaint.setShader(shader);
        canvas.drawPath(mPath, mPaint);

        return result;
    }
}
